#include "in.h"

/* for testing
carmela_tool -a create_store --store MYSTORE --log-level DEBUG
carmela_tool -a destroy_store --store MYSTORE --log-level DEBUG
carmela_tool -a create_topic --store MYSTORE --topic MYTOPIC --log-level DEBUG
carmela_tool -a destroy_topic --store MYSTORE --topic MYTOPIC --log-level DEBUG

carmela_tool -a create_store --store MYSTORE --log-level DEBUG ; carmela_tool -a create_topic --store MYSTORE --topic MYTOPIC --log-level DEBUG

carmela_tool -a produce_message --store MYSTORE --topic MYTOPIC --tags MYTAGS --msg-data MYMSGDATA --log-level DEBUG
carmela_tool -a consume_message --store MYSTORE --topic MYTOPIC --subscribe-group MYSUBSGRP --log-level DEBUG
carmela_tool -a consume_message --store MYSTORE --topic MYTOPIC --log-level DEBUG

carmela_tool -a produce_messages --store MYSTORE --topic MYTOPIC \
	--tags MYTAGS --msg-data-prefix MYMSGDATA --batch-count 10 --tcp-nodelay --log-level DEBUG
carmela_tool -a consume_messages --store MYSTORE --topic MYTOPIC \
	--subscribe-group MYSUBSGRP --batch-count 10 --tcp-nodelay --log-level DEBUG

carmela_tool -a produce_messages --store MYSTORE --topic MYTOPIC \
	--tags MYTAGS --msg-data-prefix MYMSGDATA --process-count 5 --batch-count 100 --tcp-nodelay --log-level DEBUG
carmela_tool -a consume_messages --store MYSTORE --topic MYTOPIC \
	--subscribe-group MYSUBSGRP --process-count 1 --batch-count 500 --tcp-nodelay --log-level DEBUG
carmela_tool -a consume_messages --store MYSTORE --topic MYTOPIC \
	--subscribe-group MYSUBSGRP --process-count 5 --batch-count 100 --tcp-nodelay --log-level DEBUG

carmela_tool -a produce_messages --store MYSTORE --topic MYTOPIC \
	--tags MYTAGS --msg-data-prefix MYMSGDATA --process-count 2 --batch-count 1000 --tcp-nodelay --log-level DEBUG
carmela_tool -a consume_messages --store MYSTORE --topic MYTOPIC \
	--subscribe-group MYSUBSGRP --batch-count 2000 --tcp-nodelay --log-level DEBUG

carmela_tool -a produce_messages --store MYSTORE --topic MYTOPIC \
	--tags MYTAGS --msg-data-prefix MYMSGDATA --process-count 5 --batch-count 10000 --tcp-nodelay --log-level ERROR
carmela_tool -a consume_messages --store MYSTORE --topic MYTOPIC \
	--subscribe-group MYSUBSGRP --process-count 5 --batch-count 10000 --tcp-nodelay --log-level ERROR

carmela_tool -a produce_messages --store MYSTORE --topic MYTOPIC \
	--tags MYTAGS --msg-data-prefix MYMSGDATA --process-count 5 --batch-count 100000 --tcp-nodelay --log-level ERROR
carmela_tool -a consume_messages --store MYSTORE --topic MYTOPIC \
	--subscribe-group MYSUBSGRP --process-count 5 --batch-count 100000 --tcp-nodelay --log-level ERROR

carmela_tool -a produce_delay_message --delay-seconds 5 --store MYSTORE --topic MYTOPIC --tags MYTAGS --msg-data MYMSGDATA --log-level DEBUG

carmela_tool -a produce_delay_message --delay-seconds 10 --store MYSTORE --topic MYTOPIC --tags MYTAGS --msg-data MYMSGDATA --log-level DEBUG
carmela_tool -a cancel_delay_message --store MYSTORE --delay-msg-offset 32 --log-level DEBUG

carmela_tool -s msg_file --file MYSTORE/00000000000000000000.cmqm
carmela_tool -s topic_file --file MYSTORE/MYTOPIC/00000000000000000000.cmqt
carmela_tool -s delay_msg_file --file MYSTORE/00000000000000000000.cmqd
*/

void usage()
{
	printf( "carmela_tool v%s\n" , __CARMELA_VER );
	printf( "Copyright by calvin 2021\n" );
	printf( "USAGE : carmela_tool -a [create_store|destroy_store|produce_message] --store (store_name)\n" );
	printf( "                     -a [create_topic|destroy_topic] --store (store_name) --topic (topic_name)\n" );
	printf( "                     -a produce_message --store (store_name) --topic (topic_name) [ --tags (tags) ] [ --msg-data (msg_data) ]\n" );
	printf( "                     -a consume_message --store (store_name) --topic (topic_name) [ --subscribe-group (subscribe_group_name) ]\n" );
	printf( "                     -a produce_messages --store (store_name) --topic (topic_name) [ --tags (tags) ] [ --msg-data-prefix (prefix) ] [ --process-count (count) ] --batch-count (count)\n" );
	printf( "                     -a consume_messages --store (store_name) --topic (topic_name) [ --subscribe-group (subscribe_group_name) ] [ --process-count (count) ] --batch-count (count)\n" );
	printf( "                     -a produce_delay_message --delay-seconds (seconds) --store (store_name) --topic (topic_name) [ --tags (tags) ] [ --msg-data (msg_data) ]\n" );
	printf( "                     -a cancel_delay_message --store (store_name) --delay-msg-offset (offset)\n" );
	printf( "                     [ --ip broker_ip ]\n" );
	printf( "                     [ --port broker_port ]\n" );
	printf( "                     [ --timeout (seconds) ]\n" );
	printf( "                     [ --tcp-nodelay ]\n" );
	printf( "                     -s msg_file --file (filename)\n" );
	printf( "                     -s topic_file --file (filename)\n" );
	printf( "                     -s delaymsg_file --file (filename)\n" );
	printf( "                     [ --log-pathfilenname (default for $HOME/log/carmela_tool.log) ]\n" );
	printf( "                     [ --log-level (DEBUG|INFO|WARN:default|ERROR|FATAL) ]\n" );
	return;
}

int main( int argc , char *argv[] )
{
	struct MQTool		tool ;
	int			i ;
	
	int			nret = 0 ;
	
	if( argc == 1 + 1 && STRCMP( argv[1] , == , "?" ) )
	{
		usage();
		exit(1);
	}
	
	memset( & tool , 0x00 , sizeof(struct MQTool) );
	
	for( i = 1 ; i < argc ; i++ ) 
	{
		if( STRCMP( argv[i] , == , "-a" ) && i+1 < argc )
		{
			i++;
			tool.action = argv[i] ;
		}
		else if( STRCMP( argv[i] , == , "--delay-seconds" ) && i+1 < argc )
		{
			i++;
			tool.delay_seconds = atoi(argv[i]) ;
		}
		else if( STRCMP( argv[i] , == , "--store" ) && i+1 < argc )
		{
			i++;
			tool.store_name = argv[i] ;
		}
		else if( STRCMP( argv[i] , == , "--topic" ) && i+1 < argc )
		{
			i++;
			tool.topic_name = argv[i] ;
		}
		else if( STRCMP( argv[i] , == , "--tags" ) && i+1 < argc )
		{
			i++;
			tool.tags = argv[i] ;
		}
		else if( STRCMP( argv[i] , == , "--subscribe-group" ) && i+1 < argc )
		{
			i++;
			tool.subscribe_group_name = argv[i] ;
		}
		else if( STRCMP( argv[i] , == , "--msg-data" ) && i+1 < argc )
		{
			i++;
			tool.msg_data = argv[i] ;
		}
		else if( STRCMP( argv[i] , == , "--msg-data-prefix" ) && i+1 < argc )
		{
			i++;
			tool.msg_data_prefix = argv[i] ;
		}
		else if( STRCMP( argv[i] , == , "--process-count" ) && i+1 < argc )
		{
			i++;
			tool.process_count = atoi(argv[i]) ;
		}
		else if( STRCMP( argv[i] , == , "--batch-count" ) && i+1 < argc )
		{
			i++;
			tool.batch_count = atoi(argv[i]) ;
		}
		else if( STRCMP( argv[i] , == , "--ip" ) && i+1 < argc )
		{
			i++;
			strncpy( tool.entry.ip , argv[i] , sizeof(tool.entry.ip)-1 );
		}
		else if( STRCMP( argv[i] , == , "--port" ) && i+1 < argc )
		{
			i++;
			tool.entry.port = atoi(argv[i]) ;
		}
		else if( STRCMP( argv[i] , == , "--timeout" ) && i+1 < argc )
		{
			i++;
			tool.entry.timeout_seconds = atoi(argv[i]) ;
		}
		else if( STRCMP( argv[i] , == , "--tcp-nodelay" ) )
		{
			tool.entry.tcp_nodelay = 1 ;
		}
		else if( STRCMP( argv[i] , == , "-s" ) && i+1 < argc )
		{
			i++;
			tool.show = argv[i] ;
		}
		else if( STRCMP( argv[i] , == , "--file" ) && i+1 < argc )
		{
			i++;
			tool.file = argv[i] ;
		}
		else if( STRCMP( argv[i] , == , "--delay-msg-offset" ) && i+1 < argc )
		{
			i++;
			tool.delay_msg_offset = atol(argv[i]) ;
		}
		else if( STRCMP( argv[i] , == , "--log-pathfile" ) && i+1 < argc )
		{
			i++;
			strncpy( tool.entry.log_pathfilename , argv[i] , sizeof(tool.entry.log_pathfilename)-1 );
		}
		else if( STRCMP( argv[i] , == , "--log-level" ) && i+1 < argc )
		{
			i++;
			tool.entry.log_level = ConvertLogLevel_atoi(argv[i]) ;
			if( tool.entry.log_level < 0 )
			{
				printf( "ERROR : invalid command value[%s] for '--log-level'\n" , argv[i] );
				usage();
				exit(7);
			}
		}
		else
		{
			printf( "ERROR : invalid command option[%s]\n" , argv[i] );
			usage();
			exit(7);
		}
	}
	
	if( tool.entry.log_level <= 0 )
	{
		tool.entry.log_level = LOGLEVEL_WARN ;
	}
	printf( "Set log level %s\n" , g_log_level_itoa[tool.entry.log_level] );
	
	if( STRCMP( tool.entry.log_pathfilename , == , "" ) )
	{
		snprintf( tool.entry.log_pathfilename , sizeof(tool.entry.log_pathfilename)-1 , "%s/log/carmela_tool.log" , getenv("HOME") );
	}
	printf( "Output log to %s\n" , tool.entry.log_pathfilename );
	
	MQSetLogEnv( tool.entry.log_level , tool.entry.log_pathfilename );
	
	if( STRCMP( tool.entry.ip , == , "" ) )
	{
		strncpy( tool.entry.ip , MQ_BROKER_TOOL_IP_DEFAULT , sizeof(tool.entry.ip)-1 );
	}
	INFOLOG( "tool.entry.ip[%s]" , tool.entry.ip )
	
	if( tool.entry.port <= 0 )
	{
		tool.entry.port = MQ_BROKER_PORT_DEFAULT ;
	}
	INFOLOG( "tool.entry.port[%d]" , tool.entry.port )
	
	if( tool.entry.timeout_seconds <= 0 )
	{
		tool.entry.timeout_seconds = MQ_BROKER_TIMEOUT_DEFAULT ;
	}
	INFOLOG( "tool.entry.timeout_seconds[%d]" , tool.entry.timeout_seconds )
	
	if( tool.action )
	{
		if( STRCMP( tool.action , == , "create_store" ) )
		{
			nret = app_CreateStore( & tool ) ;
			if( nret )
				return -nret;
		}
		else if( STRCMP( tool.action , == , "destroy_store" ) )
		{
			nret = app_DestroyStore( & tool ) ;
			if( nret )
				return -nret;
		}
		else if( STRCMP( tool.action , == , "create_topic" ) )
		{
			nret = app_CreateTopic( & tool ) ;
			if( nret )
				return -nret;
		}
		else if( STRCMP( tool.action , == , "destroy_topic" ) )
		{
			nret = app_DestroyTopic( & tool ) ;
			if( nret )
				return -nret;
		}
		else if( STRCMP( tool.action , == , "produce_message" ) )
		{
			nret = app_ProduceMessage( & tool ) ;
			if( nret )
				return -nret;
		}
		else if( STRCMP( tool.action , == , "consume_message" ) )
		{
			nret = app_ConsumeMessage( & tool ) ;
			if( nret )
				return -nret;
		}
		else if( STRCMP( tool.action , == , "produce_messages" ) )
		{
			nret = app_ProduceMessages( & tool ) ;
			if( nret )
				return -nret;
		}
		else if( STRCMP( tool.action , == , "consume_messages" ) )
		{
			nret = app_ConsumeMessages( & tool ) ;
			if( nret )
				return -nret;
		}
		else if( STRCMP( tool.action , == , "produce_delay_message" ) )
		{
			nret = app_ProduceDelayMessage( & tool ) ;
			if( nret )
				return -nret;
		}
		else if( STRCMP( tool.action , == , "cancel_delay_message" ) )
		{
			nret = app_CancelDelayMessage( & tool ) ;
			if( nret )
				return -nret;
		}
		else
		{
			usage();
			exit(7);
		}
	}
	else if( tool.show )
	{
		if( STRCMP( tool.show , == , "msg_file" ) )
		{
			nret = app_ParseMessageFile( & tool ) ;
			if( nret )
				return -nret;
		}
		else if( STRCMP( tool.show , == , "topic_file" ) )
		{
			nret = app_ParseTopicFile( & tool ) ;
			if( nret )
				return -nret;
		}
		else if( STRCMP( tool.show , == , "delay_msg_file" ) )
		{
			nret = app_ParseDelayMessageFile( & tool ) ;
			if( nret )
				return -nret;
		}
		else
		{
			usage();
			exit(7);
		}
	}
	else
	{
		usage();
		exit(7);
	}
	
	return 0;
}

